package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.PlanVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.PlanConstant;
import com.bbzn.device.client.constant.TimeConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dataobject.WorkPlan;
import com.bbzn.device.client.dto.index.PlanDTO;
import com.bbzn.device.client.dto.index.PlanInfoDTO;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2022/1/17 14:32
 * @Version 1.0
 */

@RestController
@RequestMapping("/plan")
@Slf4j
public class PlanController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private WorkPlanService workPlanService;


    /**
     * 添加工作计划
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "添加工作计划")
    public BaseResponse addPlan(@RequestBody PlanVO planVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(planVO.getPlanName()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_NAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planVO.getDuration()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_DURATION_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planVO.getLeader()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_LEADER_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planVO.getStartTime()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(TimeConstant.TIME_ENUM, TimeConstant.TIME_NO_START_TIME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planVO.getPid()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_PID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        WorkPlan plan = new WorkPlan();
        plan.setUserId(userId);
        plan.setPlanName(planVO.getPlanName());
        List<WorkPlan> workPlanList = workPlanService.findByAll(plan);
        if (CollectionUtils.isNotEmpty(workPlanList)) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_NAME_EXIST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        WorkPlan insert = BabaBeanUtils.voToDto(planVO, WorkPlan.class);
        insert.setUserId(userId);
        insert.setState(0);
        insert.setCreateTime(new Date());
        insert.setCreatedBy(username);
        workPlanService.insertSelective(insert);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询工作计划
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPlanList", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询工作计划")
    public BaseResponse getPlanList(HttpServletRequest request) {
        Long userId = commonService.getUserId(request);
        List<PlanDTO> plans = workPlanService.findAllPlanByUserId(userId);
        return setResultSuccess(plans);
    }

    /**
     * 查询计划列表-平级显示
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPlans", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询计划列表-平级显示")
    public BaseResponse getPlans(HttpServletRequest request) {
        Long userId = commonService.getUserId(request);
        List<PlanInfoDTO> plans = workPlanService.findPlanInfoByUserId(userId);
        return setResultSuccess(plans);
    }

    /**
     * 修改工作计划
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updatePlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "修改工作计划")
    public BaseResponse updatePlan(@RequestBody PlanVO planVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(planVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        WorkPlan plan = BabaBeanUtils.voToDto(planVO, WorkPlan.class);
        plan.setUpdateTime(new Date());
        plan.setUpdatedBy(username);
        workPlanService.updateByPrimaryKeySelective(plan);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 删除工作计划
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "删除工作计划")
    public BaseResponse delPlan(@RequestBody PlanVO planVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(planVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        workPlanService.delPlanById(userId,planVO.getId());
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

}
